package com.cyh.core.framework.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Component
public class RestTemplateUtil<T> {

    @Autowired
    RestTemplate restTemplate;

    private RestTemplateUtil(){}

    public T postForEntity(String url,Map<String, Object> requestBody,Class<T> Myclass){
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(requestBody, headers);
        return JsonUtil.jsonToPojo(restTemplate.postForEntity(url,httpEntity,String.class).getBody(),Myclass);
    }

    public T postForEntity(String url,HttpHeaders headers,Class<T> Myclass){
        Map<String, Object> requestBody=new HashMap<>();
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(requestBody, headers);
        return JsonUtil.jsonToPojo(restTemplate.postForEntity(url,httpEntity,String.class).getBody(),Myclass);
    }

    public T postForEntity(String url,HttpHeaders headers,Map<String, Object> requestBody,Class<T> Myclass){
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(requestBody, headers);
        return JsonUtil.jsonToPojo(restTemplate.postForEntity(url,httpEntity,String.class).getBody(),Myclass);
    }


}
